/*
 ============================================================================
 Name        : FileHandler.c
 Author      : Sherry Krikszer
 Version     : 1.0
 Copyright   :
 Description : in C, Ansi-style
 ============================================================================
 */

#include <stdlib.h>
#include <stdio.h>

#include "FileHandler.h"

/* ----------------------------------------------------------------------------
 *	Constants Definition
 * ----------------------------------------------------------------------------*/

#define READ_MODE "r"

#define NEW_LINE_FILTER "%[^'\n']"

/* ----------------------------------------------------------------------------
 *	Constants Definition End
 * ----------------------------------------------------------------------------*/

/* ----------------------------------------------------------------------------
 *	Function Declaration
 * ----------------------------------------------------------------------------*/

FileLines* InitFileLines();

/* ----------------------------------------------------------------------------
 *	Function Declaration End
 * ----------------------------------------------------------------------------*/

/* ----------------------------------------------------------------------------
 *	Function Definition
 * ----------------------------------------------------------------------------*/

/* TODO : write documentation for this method */
FileLines* ReadLinesFromFile(const char *fileName)
{
	FILE* fp = NULL;

	FileLines* currFileLines = NULL;

	/* In case that the given file open succeeded */
	if ((fp = fopen(fileName, READ_MODE)) != NULL)
	{
		currFileLines = InitFileLines();

		/* In case file lines initializing succeeded */
		if (currFileLines != NULL)
		{
			char lineFromFile[MAX_LINE_LENGTH];

			/* Go over all the lines in the file and save them */
			while(fgets(lineFromFile, MAX_LINE_LENGTH, fp) != NULL)
			{
				/* Save the file line without the new line character */
				sscanf(lineFromFile, NEW_LINE_FILTER,
						currFileLines->FileLine[currFileLines->lineCounter].line);
				/* Advance to the next line */
				(currFileLines->lineCounter)++;
			}

//			/* Go over all the lines in the file and save them */
//			while(fgets(currFileLines->FileLine[currFileLines->lineCounter].line,
//					MAX_LINE_LENGTH, fp) != NULL)
//			{
//				/* Advance to the next line */
//				(currFileLines->lineCounter)++;
//			}

			fclose(fp);
		}
	}
	else
	{
		printf("Cannot open %s\n", fileName);
	}

	return currFileLines;
}

/* TODO : write documentation for this method */
FileLines* InitFileLines()
{
	FileLines* newFileLines = (FileLines*) malloc (sizeof(FileLines));
	int i = 0;
	if (newFileLines != NULL)
	{
		newFileLines->lineCounter = 0;
	}

	for (i = 0; i < MAX_FILE_LINE_COUNT; i++)
	{
		newFileLines->FileLine[i].line[0] = '\0';
	}
	return newFileLines;
}

/* TODO : write documentation for this method */
void FreeFileLines(FileLines* fileLines)
{
	free(fileLines);
}

/* TODO : write documentation for this method */
Boolean WriteFile(const char *fileName, FileLines* lineList)
{
	return FALSE;
}

/* ----------------------------------------------------------------------------
 *	Function Definition End
 * ----------------------------------------------------------------------------*/
